<template>
  <div>
    <el-dialog
      title="新增目录"
      :visible="editSubjectDialog"
      width="400px"
      @close="onClose"
      >
      <el-form ref="form" :model="form" :rules="rules">
      <el-form-item v-if="!$route.query.id" label="所属学科"  >
        <el-select v-model="form.subjectID" placeholder="请选择">
          <el-option
            v-for="item in options"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="目录名称" prop="directoryName" >
        <el-input size="small" v-model.trim="form.directoryName"  placeholder="请输入目录名称"></el-input>
      </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="onClose">取 消</el-button>
        <el-button type="primary" @click="editDirectorysName">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { add, list } from '@/api/hmmm/directorys.js'
import { simple } from '@/api/hmmm/subjects.js'
export default {
  data () {
    return {
      form: {
        subjectID: '',
        directoryName: ''
      },
      value: true,
      rules: {
        directoryName: [{ required: true, message: '请输入标签名称', trigger: 'blur' }
        ]
      },
      subjectID: '',
      options: [],
      directorysNameList: [],
      flag: false
    }
  },
  props: {
    // 对话框显示隐藏
    editSubjectDialog: {
      type: Boolean,
      required: true
    }
    // 每一行的数据
    // directorysInfo: {
    //   type: Object,
    //   required: true
    // },
  },
  // computed: {
  //   directorysNameList () {
  //     return this.directorysList
  //   }
  // },
  created () {
    this.getSubjectList()
  },

  methods: {
    // 添加标签
    async editDirectorysName () {
      try {
        await this.$refs.form.validate()
        if (this.$route.query.id) {
          await this.getDirectorysList({ subjectID: this.$route.query.id })
          if (this.flag) {
            this.$message.error('该目录已存在')
            return false
          }
          this.form.subjectID = Number(this.$route.query.id)
          await add(this.form)
        } else {
          await this.getDirectorysList({})
          if (this.flag) {
            this.$message.error('该目录已存在')
            return false
          }
          this.subjectID = this.form.subjectID
          await add({ subjectID: this.subjectID, ...this.form })
        }
        this.$emit('getDirectory')
        this.$emit('update:editSubjectDialog', false)
        this.$message.success('亲，新增成功哟~')
        this.onClose()
      } catch (err) {
      }
    },
    // 关闭弹框
    onClose () {
      this.$emit('update:editSubjectDialog', false)
      this.$refs.form.resetFields()
      this.form.directoryName = ''
    },
    // 获取所属学科
    async getSubjectList () {
      const { data } = await simple()
      this.options = data
    },
    // 获取目录列表
    async getDirectorysList (subjectID) {
      const res = await list(subjectID)
      this.directorysNameList = res.data.items.map(item => item.directoryName)
      if (this.directorysNameList !== []) {
        this.flag = (this.directorysNameList.some(item => {
          return item === this.form.directoryName
        }))
      }
    }
  }
}
</script>

<style scoped lang='less'>
.el-form-item {
  /deep/.el-table__body-wrapper{
    /deep/.el-table__body{
      /deep/.el-form-item__label {
      width: 80px;
      }
    }
  }
  }
/deep/.el-form-item__content {
  margin-left: 80px;
}
  /deep/.el-input {
    display: flex;
    width: 280px !important;
  }
</style>
